package cn.yeyiu.sexyiu.fragment;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.io.File;

import cn.yeyiu.sexyiu.adapter.DownloadVideoAdapter;
import cn.yeyiu.sexyiu.listener.OnItemClickListener;
import cn.yeyiu.util.ExternalJSONFile;
import cn.yeyiu.util.MediaUtil;
import cn.yeyiu.util.NetUtil;
import cn.yeyiu.util.StringUtil;
import cn.yeyiu.util.bean.VideoInfo;

/**
 * 基于Fragment实现 tablayout + viewpager2
 */

public class DownloadFragment extends Fragment {


    private final String type;
    private final Context mContext;
    private RecyclerView rv;

    public DownloadFragment(Context context, String type) {
        this.mContext = context;
        this.type = type;
    }

    void setView(){
        rv = new RecyclerView(mContext);
        rv.setLayoutParams(new RecyclerView.LayoutParams(
                RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.MATCH_PARENT));
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        rv.setLayoutManager(linearLayoutManager);

        DownloadVideoAdapter adapter = new DownloadVideoAdapter(mContext, type);
        rv.setAdapter(adapter);

        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view) {
                int childAdapterPosition = rv.getChildAdapterPosition(view);
                VideoInfo videoInfo = adapter.dataList.get((adapter.dataList.size() - 1) - childAdapterPosition);

                String fileMd5 = StringUtil.md5(videoInfo.title)+videoInfo.videoSrc.substring(videoInfo.videoSrc.length()-4);
                String path = mContext.getExternalFilesDir("").getPath() + "/download/" + type + "/" + fileMd5;
                File f = new File(path);
                if (f.exists()) {
                    MediaUtil.playLocalVideo(mContext, new File(path));
                } else {
                    new AlertDialog.Builder(mContext)
                            .setTitle("文件不存在！")
                            .setMessage(videoInfo.title + "\n\t\t>> 你可以...")
                            .setPositiveButton("重新下载", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    NetUtil.downloadVideo(mContext, videoInfo);
                                }
                            })
                            .setNegativeButton("删除记录", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    ExternalJSONFile.remove(mContext, "download_" + type, videoInfo.title);
                                    adapter.notifyItemRemoved(childAdapterPosition);
                                }
                            }).create().show();
                }
            }

            @Override
            public void onItemLongClick(View view) {
                int childAdapterPosition = rv.getChildAdapterPosition(view);
                int size = (adapter.dataList.size() - 1) - childAdapterPosition;
                VideoInfo videoInfo = adapter.dataList.get(size);

                new AlertDialog.Builder(mContext).setTitle("删除确认")
                        .setMessage("你确定要删除：" + videoInfo.title)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                ExternalJSONFile.remove(mContext, "download_" + type, size);
                                adapter.dataList.remove(size);
                                adapter.notifyItemRemoved(childAdapterPosition);
                            }
                        })
                        .setNegativeButton("取消",null)
                        .create().show();
            }
        });
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        setView();
        return rv;
    }
}